package mwa.cs.admin.ctrl;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import mwa.cs.dao.BackTimeDAO;
import mwa.cs.dao.ReceivePositionDAO;
import mwa.cs.dao.SitesDAO;
import mwa.cs.dao.UserLoginDAO;
import mwa.cs.database.Database;
import mwa.cs.model.BackTime;
import mwa.cs.model.MailProperties;
import mwa.cs.model.ReceivePosition;
import mwa.cs.util.JDate;
import mwa.cs.util.Mail;

public class CS84000Srvl extends HttpServlet implements Servlet {
	
	private Database db;
	private int vnSiteId;
	private int vnUserId;


    public CS84000Srvl() {
        super();
    }
    
    public void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
    	
    	String vaProcessType = request.getParameter("process_type");
    	
		if(vaProcessType.equals("resetPassword")){
			resetPassword(request,response);
		}
    }

    public void resetPassword(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
    	
    	request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8"); 
		response.setContentType("text/JSON");
		
		vnSiteId = Integer.parseInt(request.getSession().getAttribute("site_id").toString());
		vnUserId = Integer.parseInt(request.getSession().getAttribute("user_id").toString());
		
		String vaUserName = request.getParameter("userName");

		PrintWriter voOut = response.getWriter();
		
		try
		{
			this.db = new Database();
		}
		catch(Exception ex)
		{
			voOut.print("connect_db_fail");
			return;
		}
		
		UserLoginDAO voDaoUserLogin = new UserLoginDAO(db);
		Map<String,Object> voObjUser = voDaoUserLogin.findUserByUserName(vaUserName);
		
		if(voObjUser == null || voObjUser.size() == 0)
		{
			db.close();
			voOut.print("no_user");
		}
		else
		{
			Mail voMail = new Mail();
			MailProperties voMailProperties = new MailProperties();
			

			String vaMailSubject = "มีคนขอรหัสผ่านใหม่ของบัญชีผู้ใช้ ระบบบริหารงานประปา ของคุณ";
			String vaMailtext = "มีคนขอให้คุณตั้งรหัสผ่านบัญชีผู้ใช้  ระบบบริหารงานประปา  ของคุณใหม่\n"
					+ "<!DOCTYPE html>"
					+ "<html>"
					+ "<head>"
					+ "<meta http-equiv='Content-Type' content='text/html; charset=UTF-8' />"
					+ "<meta content='width=device-width, initial-scale=1.0' name='viewport' />"
					+ "</head>"
					+ "<body>"
					+ "<a href='http://" + request.getLocalAddr() + ":" + request.getLocalPort() + "/CS/recover/RecoverPasswordSrvl?UserId="+voObjUser.get("USER_ID").toString()+"&Key="+voObjUser.get("PASSWORD_KEY").toString()+"'>"
					+ "<button style='font-family:Helvetica Neue,Helvetica,Lucida Grande,tahoma,verdana,arial,sans-serif;font-weight:bold;font-size:14px;color:#ffffff;background-color: #428bca;border-color: #357ebd;text-shadow:0 1px 0 #415686;display: inline-block;margin-bottom: 0;font-weight: 400;text-align: center;vertical-align: middle;cursor: pointer;background-image: none;border: 1px solid transparent;white-space: nowrap;padding: 6px 12px;font-size: 14px;line-height: 1.428571429;border-radius: 4px;-webkit-user-select: none;-moz-user-select: none;-ms-user-select: none;-o-user-select: none;user-select: none;'>"
					+ "คลิ๊กที่นี่เพื่อกำหนดรหัสผ่านใหม่"
					+ "</button>"
					+ "</a>"
					+ "</body>"
					+ "</html>";
			
			voMailProperties.setVaMailAuth("false");
			voMailProperties.setVaMailHost("smtp.gmail.com");
			voMailProperties.setVaMailPort("465");
			voMailProperties.setVaMailReceiver(voObjUser.get("EMAIL_ADDRESS").toString());
			voMailProperties.setVaMailSender("thapakit2@gmail.com");
			voMailProperties.setVaMailSenderPassword("The-Thapakit");
			voMailProperties.setVaMailSubject(vaMailSubject);
			voMailProperties.setVaMailText(vaMailtext);
			
			try
			{
				voMail.sendFromGmail(voMailProperties);				
				voOut.print("success");
			}
			catch(Exception ex)
			{
				voOut.print("fail");
			} finally{
				db.close();
			}
			
		}
    }
    
    
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request,response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request,response);
	}
}
